//
//  ZKEryptionHandle.m
//  朱凯的工具箱
//
//  Created by apple on 15/12/3.
//  Copyright © 2015年 zhukai. All rights reserved.
//

#import "ByCoreCrypt.h"


@implementation ByCoreCrypt

+ (NSString *)pay_crypt:(NSString *)str {
    if (str == nil || str.length == 0) {
        return @"";
    }
    
    
    str = [[str stringByEncodingURLFormat] lowercaseString];
    
    str = [self md5:str];
    
    str = [self sha1: str];
    
    str = [NSString stringWithFormat:@"%@ITBOYE_PAY_DASFSAFKL",str];
    
    return [self md5:str];
}


+(NSString *)sha1:(NSString *)text{
    
    const char *cStr = [text UTF8String];
    
    NSData *data = [NSData dataWithBytes:cStr length:text.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(data.bytes, (unsigned int)data.length, digest);
    
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++){
        [output appendFormat:@"%02x", digest[i]];
    }
    
    return output;
}


+ (NSString *)md5:(NSString *)text {
    const char *cStr = [text UTF8String];
    unsigned char result[16];
    
    CC_MD5(cStr, (unsigned int)strlen(cStr), result); // This is the md5 call
    
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

+ (NSString *)jsonEncode:(id)obj {
    
    NSString *jsonString;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj options:NSJSONWritingPrettyPrinted error:&error];
    
    if (!jsonData) {
        NSLog(@"byit____Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    return jsonString;
}

+ (id)jsonDecode:(NSString *)jsonStr {
    
    NSData* data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError* error = nil;
    
    // NSJSONReadingAllowFragments:指定解析器应该允许不属于的NSArray或NSDictionary中的实例顶层对象
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    
    if (error != nil) {
        return nil;
    }
    
    return result;
}

+(NSString *)base64Encode:(NSString *)jsonStr{
    
    NSData* originData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString* encodeResult = [originData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    
    return encodeResult;
}

+(NSString *)base64Decode:(NSString *)encodeStr{
    
    NSData* decodeData = [[NSData alloc] initWithBase64EncodedString:encodeStr options:0];
    
    NSString* decodeStr = [[NSString alloc] initWithData:decodeData encoding:NSUTF8StringEncoding];
    
    return decodeStr;
}

/**
 *  获得最终加密字符串
 *
 *  @param base64Str 经过两次base64加密后的字符串
 *
 *  @return 最终加密字符串
 */
+ (NSString *)encryptString:(id)obj {
    
    NSString *jsonStr = nil;
    if (obj == nil) { // 如果传入的的业务参数数空，则用一个空的json字符串做base64加密
        jsonStr = @"{}";
    } else {
        jsonStr = [self jsonEncode:obj];
    }
    NSString *base64Str = [self base64Encode:jsonStr];
    
    base64Str = [self base64Encode:base64Str];

    return base64Str;
}

/**
 *  获取最原始的数据
 *
 *  @param finalStr 最终加密的字符串
 *
 *  @return 最原始的数据
 */
+ (id)decryptString:(NSString *)finalStr {
    
    NSString *base64Str = [self base64Decode:finalStr];
    
    base64Str = [self base64Decode:base64Str];
    
    id obj = [self jsonDecode:base64Str];
    
    return obj;
}

/**
 *  对传输的POST数据签名
 *
 *  @param data data字典
 *
 *  @return 签名
 */
+ (NSString *)signData:(NSDictionary *)dicData {
    
    if (dicData == nil ||
        dicData.allKeys.count == 0 ||
        (NSNull *)dicData == [NSNull null]) {
        
        return @"";
    }
    
    NSString *time = [dicData valueForKey:@"time"];
    NSString *type = [dicData valueForKey:@"type"];
    NSString *data = [dicData valueForKey:@"data"];
    NSString *client_secret = BOYE_CLIENT_SECRET;
    NSString *notify_id = [dicData valueForKey:@"notify_id"];
    
    NSString *totalStr = [NSString stringWithFormat:@"%@%@%@%@%@", time, type, data, client_secret, notify_id];

    return [self md5:totalStr];
}
@end
